package pokeronlab_2014;


import java.util.Random;

import com.biotools.meerkat.Holdem;import pokeronlab_2014.ExpertSystemData.Position;


public class ExpertSystem {

	/**
	 * 
	 */
	public ExpertSystemData data;
	pokeronlab_2014.ExpertSystemData.handStrength handstrength;
	pokeronlab_2014.ExpertSystemData.Actions actions;
	pokeronlab_2014.ExpertSystemData.Position position;
	Database database;
	int actionID = 0;
	double foldchance;
	double checkchance;
	double callchance;
	double raisechance;

	/**
	 * 
	 */
	public ExpertSystem() {
		//data = new ExpertSystemData();
		database = Database.getInstance();
	}

	/**
	 * @return
	 */
	public int getAction() {
		actionID = Holdem.FOLD;
		data = database.getExpertSystemData();
		handstrength = data.getHandStrength();
		actions = data.getActions();
		position = data.getPosition();
		// kezek szerinti csoportositas
		switch (handstrength) {
		// nagyon eros kez
		case VERY_STRONG:
			raisechance = 40;
			callchance = 10;
			checkchance = 5;
			foldchance = 0.5;
			actionID = getValueFromChance(raisechance, callchance, checkchance,
					foldchance, data.pos);
			break;
		// eros kez
		case STRONG: {
			switch (actions) {
			case ONE_RAISE_CALLS:
				raisechance = 8;
				callchance = 20;
				checkchance = 2;
				foldchance = 0.5;
				actionID = getValueFromChance(raisechance, callchance,
						checkchance, foldchance, data.pos);
				;
				break;

			case ONE_RAISE:
				if (position == Position.EARLY) {
					raisechance = 1;
					callchance = 3;
					checkchance = 9;
					foldchance = 18;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 20;
					callchance = 9;
					checkchance = 2;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			default: {
				raisechance = 19;
				callchance = 7;
				checkchance = 2;
				foldchance = 1;
				actionID = getValueFromChance(raisechance, callchance,
						checkchance, foldchance, data.pos);
			}
				break;
			}
			break;
		}
		// kozepesen eros kez
		case MEDIOCRE: {
			switch (actions) {
			case ALL_FOLD:
				if (position == Position.EARLY) {
					raisechance = 1;
					callchance = 3;
					checkchance = 8;
					foldchance = 16;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 19;
					callchance = 8;
					checkchance = 3;
					foldchance = 2;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_CALL:
				if (position == Position.EARLY) {
					raisechance = 1;
					callchance = 4;
					checkchance = 11;
					foldchance = 20;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 18;
					callchance = 10;
					checkchance = 4;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case MORE_CALL:
				if (position == Position.EARLY) {
					raisechance = 1;
					callchance = 3;
					checkchance = 8;
					foldchance = 19;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 18;
					callchance = 11;
					checkchance = 5;
					foldchance = 2;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_RAISE:
				if (position == Position.BIG_BLIND) {
					raisechance = 9;
					callchance = 20;
					checkchance = 10;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 1;
					callchance = 2;
					checkchance = 7;
					foldchance = 19;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_RAISE_CALLS:
				if (position == Position.BIG_BLIND) {
					raisechance = 2;
					callchance = 18;
					checkchance = 10;
					foldchance = 0.5;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 0.5;
					callchance = 2;
					checkchance = 3;
					foldchance = 20;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			}
			break;
		}
		case SPECULATIVE: {
			switch (actions) {
			case ALL_FOLD:
				if (position == Position.EARLY || position == Position.MIDDLE) {
					raisechance = 1;
					callchance = 2;
					checkchance = 9;
					foldchance = 19;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 20;
					callchance = 8;
					checkchance = 3;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_CALL:
				if (position == Position.EARLY || position == Position.MIDDLE) {
					raisechance = 1;
					callchance = 2;
					checkchance = 8;
					foldchance = 19;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else if (position == Position.LATE || position == Position.SMALLBLIND) {
					raisechance = 3;
					callchance = 19;
					checkchance = 5;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 2;
					callchance = 10;
					checkchance = 17;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case MORE_CALL:
				if (position == Position.BIG_BLIND) {
					raisechance = 2;
					callchance = 8;
					checkchance = 19;
					foldchance = 0.5;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 3;
					callchance = 19;
					checkchance = 10;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_RAISE:
				if (position == Position.BIG_BLIND) {
					raisechance = 4;
					callchance = 19;
					checkchance = 9;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 1;
					callchance = 2;
					checkchance = 7;
					foldchance = 18;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_RAISE_CALLS:
				raisechance = 2;
				callchance = 18;
				checkchance = 10;
				foldchance = 4;
				actionID = getValueFromChance(raisechance, callchance,
						checkchance, foldchance, data.pos);
				break;
			}
			break;
		}

		case MIXED: {
			switch (actions) {
			case ALL_FOLD:
				if (position == Position.EARLY || position == Position.MIDDLE) {
					raisechance = 1;
					callchance = 2;
					checkchance = 10;
					foldchance = 20;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 18;
					callchance = 9;
					checkchance = 3;
					foldchance = 1;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_CALL:
				if (position == Position.EARLY || position == Position.MIDDLE
						|| position == Position.LATE) {
					raisechance = 1;
					callchance = 2;
					checkchance = 9;
					foldchance = 20;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else if (position == Position.SMALLBLIND) {
					raisechance = 1;
					callchance = 18;
					checkchance = 9;
					foldchance = 3;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 1;
					callchance = 15;
					checkchance = 20;
					foldchance = 6;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case MORE_CALL:
				if (position == Position.EARLY || position == Position.MIDDLE) {
					raisechance = 0.5;
					callchance = 2;
					checkchance = 4;
					foldchance = 20;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else if (position == Position.LATE || position == Position.SMALLBLIND) {
					raisechance = 1;
					callchance = 20;
					checkchance = 10;
					foldchance = 4;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				} else {
					raisechance = 1;
					callchance = 10;
					checkchance = 20;
					foldchance = 3;
					actionID = getValueFromChance(raisechance, callchance,
							checkchance, foldchance, data.pos);
				}
				break;

			case ONE_RAISE:
				raisechance = 1;
				callchance = 3;
				checkchance = 5;
				foldchance = 19;
				actionID = getValueFromChance(raisechance, callchance,
						checkchance, foldchance, data.pos);
				break;

			case ONE_RAISE_CALLS:
				raisechance = 0.5;
				callchance = 1;
				checkchance = 4;
				foldchance = 19;
				actionID = getValueFromChance(raisechance, callchance,
						checkchance, foldchance, data.pos);
				break;
			}
			break;
		}
		case OTHER:
			raisechance = 0.02;
			callchance = 0.5;
			checkchance = 2;
			foldchance = 20;
			actionID = getValueFromChance(raisechance, callchance,
					checkchance, foldchance, data.pos);
			break;
		}
		Controller.debugWindow.printMessage("expersystem dontes");
		return actionID;
	}

	public int getValueFromChance(double raisechance, double callchance,
			double checkchance, double foldchance, int pos) {
		int actionID = 0;
		double rc = raisechance
				/ (raisechance + callchance + checkchance + foldchance);
		double cc = callchance
				/ (raisechance + callchance + checkchance + foldchance);
		double chc = checkchance
				/ (raisechance + callchance + checkchance + foldchance);
		Random random = new Random();
		double r = random.nextDouble();
		if (rc > r
				&& Controller.thisGameInfo.canRaise(Controller.thisGameInfo
						.getCurrentPlayerSeat()))
			actionID = Holdem.RAISE;
		else if (rc + cc > r
				&& Controller.thisGameInfo.getAmountToCall(pos) > 0)
			actionID = Holdem.CALL;
		else if (rc + cc + chc > r)
			actionID = Holdem.CHECK;
		else
			actionID = Holdem.FOLD;
		return actionID;
	}
}
